package com.letv.whatslive.model.convert;

import com.letv.whatslive.common.utils.ObjectUtils;
import com.letv.whatslive.model.Firmware;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by liyulin on 2016/1/15.
 */
public class FirmwareConvert {
    public static Firmware castDBObjectToFirmware(DBObject dbObject) {
        Firmware firmware = new Firmware();
        if (dbObject != null) {
            firmware.setId(ObjectUtils.toLong(dbObject.get("_id")));
            firmware.setAppVer(ObjectUtils.toString(dbObject.get("appVer")));
            firmware.setModel(ObjectUtils.toString(dbObject.get("model")));
            firmware.setSysVer(ObjectUtils.toString(dbObject.get("sysVer")));
            firmware.setPcode(ObjectUtils.toString(dbObject.get("pcode")));
            firmware.setFwMD5(ObjectUtils.toString(dbObject.get("fwMD5")));
            firmware.setFwModel(ObjectUtils.toString(dbObject.get("fwModel")));
            firmware.setFwName(ObjectUtils.toString(dbObject.get("fwName")));
            firmware.setFwSystem(ObjectUtils.toString(dbObject.get("fwSystem")));
            firmware.setFwVersion(ObjectUtils.toString(dbObject.get("fwVersion")));
            firmware.setFwUrl(ObjectUtils.toString(dbObject.get("fwUrl")));
            firmware.setFwVersionInt(ObjectUtils.toInteger(dbObject.get("fwVersionInt")));
            firmware.setAppVerInt(ObjectUtils.toInteger(dbObject.get("appVerInt")));
            firmware.setFwFlag(ObjectUtils.toInteger(dbObject.get("fwFlag")));
        }
        return firmware;
    }

    public static DBObject castfirmwareToDBObject(Firmware firmware) {
        DBObject dbo = new BasicDBObject();
        if (firmware.getId() != null) {
            dbo.put("_id", firmware.getId());
        }

        if (StringUtils.isNotBlank(firmware.getAppVer())) {
            dbo.put("appVer", firmware.getAppVer());
            dbo.put("appVerInt", formatVersion(firmware.getAppVer()));
        } else {
            dbo.put("appVer", "");
            dbo.put("appVerInt",-1);
        }
        if (StringUtils.isNotBlank(firmware.getModel())) {
            dbo.put("model", firmware.getModel());
        } else {
            dbo.put("model", "");
        }
        if (StringUtils.isNotBlank(firmware.getSysVer())) {
            dbo.put("sysVer", firmware.getSysVer());
        } else {
            dbo.put("sysVer", "");
        }
        if (StringUtils.isNotBlank(firmware.getPcode())) {
            dbo.put("pcode", firmware.getPcode());
        } else {
            dbo.put("pcode", "");
        }
        if (StringUtils.isNotBlank(firmware.getFwMD5())) {
            dbo.put("fwMD5", firmware.getFwMD5());
        } else {
            dbo.put("fwMD5", "");
        }
        if (StringUtils.isNotBlank(firmware.getFwModel())) {
            dbo.put("fwModel", firmware.getFwModel());
        } else {
            dbo.put("fwModel", "");
        }
        if (StringUtils.isNotBlank(firmware.getFwName())) {
            dbo.put("fwName", firmware.getFwName());
        } else {
            dbo.put("fwName", "");
        }
        if (StringUtils.isNotBlank(firmware.getFwSystem())) {
            dbo.put("fwSystem", firmware.getFwSystem());
        } else {
            dbo.put("fwSystem", "");
        }
        if (StringUtils.isNotBlank(firmware.getFwVersion())) {
            dbo.put("fwVersion", firmware.getFwVersion());
            dbo.put("fwVersionInt",formatVersion(firmware.getFwVersion()));
        } else {
            dbo.put("fwVersion", "");
            dbo.put("fwVersionInt",-1);
        }
        if (StringUtils.isNotBlank(firmware.getFwUrl())) {
            dbo.put("fwUrl", firmware.getFwUrl());
        } else {
            dbo.put("fwUrl", "");
        }
        if (firmware.getFwFlag() != null) {
            dbo.put("fwFlag", firmware.getFwFlag());
        }else {
            dbo.put("fwFlag", 1);
        }


        return dbo;
    }

    /**
     * 转换版本号2.3.1为203010
     * e.g. 2.3.1 = 2*10^4+3*10^2+1
     *
     * @param version
     * @return
     */
    public static int formatVersion(String version) {
        if (version == null || version.isEmpty()) {
            return -1;
        }
        String[] verStrs = version.split("\\.");
        if (verStrs.length > 3) {
            return -1;
        }
        int versionInt = 0;
        int times = 4;
        for (int i = 0; i < verStrs.length; i++) {
            versionInt = versionInt + Integer.parseInt(verStrs[i]) * (int) Math.pow(10, times);
            times = times - 2;
        }
        return versionInt;
    }
}
